#include "Catch2.v3/catch_amalgamated.hpp"
#include "NetWork/ThreadPool.h"

#include <iostream>
#include <chrono>

#define Max_Num 10000000

TEST_CASE("NetWork::ThreadPool") {
    NetWork::ThreadPool threadpool(16, 1024, true);
    std::vector<std::future<size_t>> futures;

    auto start = std::chrono::steady_clock::now();
    

    size_t result = 0;
    for (int i = 1; i <= Max_Num; i++)
        result += i;

    auto end = std::chrono::steady_clock::now();
    auto temp = std::chrono::duration_cast<std::chrono::duration<double>>(end  - start);
    std::cout << "result: " << result << std::endl;
    std::cout << "cost:   " <<  temp.count() << std::endl;
    
    for (int i = 0; i < 10; i++) {
        size_t min = i * (Max_Num / 10) + 1;
        size_t max = min + (Max_Num / 10);
        futures.push_back(threadpool.AddTask([](size_t min, size_t max) {
            size_t result = .0f;
            for (size_t i = min; i < max; i++)
                result += i;

            return result;
        }, min, max));
    }

    result = 0;
    for (auto& f: futures)
        result += f.get();

    auto end1 = std::chrono::steady_clock::now();
    temp = std::chrono::duration_cast<std::chrono::duration<double>>(end1 - end);
    std::cout << "result: " << result << std::endl;
    std::cout << "cost:   " <<  temp.count() << std::endl;
}